<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Monhoc_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
		
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
	function get_all_monhoc($limit, $start)
	{
		$this->db->select('monhoc.*');
		$this->db->from('monhoc');
		$this->db->where('monhoc.deleted',0);
		$this->db->order_by('monhoc.id','desc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
	}	
	
	function get_tenkhoa()
	{
		$this->db->select('id, makhoa, tenkhoa');
		$this->db->from('khoa');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_giaovien()
	{
		$this->db->select('id, magv, tengv');
		$this->db->from('giaovien');
		$this->db->where('status',1);
		$this->db->where('deleted',0);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Get all entries
	 *
	 * @access	public
	 * @return 	object	Entries
	 */
	function add_monhoc()
	{
		$data = array(
			'mamon' 		=> $this->input->post('mamon'),
			'tenmon'		=> $this->input->post('tenmon'),
			'sotinchi'		=> $this->input->post('sotinchi'),
			'id_khoa'		=> $this->input->post('makhoa'),
			'created_by'	=> $this->user->id,
			'status'		=> $this->input->post('status'),
			'deleted'		=> 0,
		);
		
		$this->db->set($data);
		$this->db->insert('monhoc');
	}
	
	
	public function check_mamon()
	{
		$mamon = $this->input->post('mamon');
        $query =  "
			SELECT id, mamon
			FROM monhoc WHERE mamon ='$mamon' 
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function check_cmnd()
	{
        $this->db->where('cmnd',$this->input->post('cmnd'));
        $query = $this->db->get('giaovien');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	
	public function check_email()
	{
        $this->db->where('email',$this->input->post('email'));
        $query = $this->db->get('giaovien');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function get_monhoc_info($id)
	{
		$this->db->select('*');
		$this->db->from('monhoc');
		$this->db->where('id', $id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function edit_monhoc($id)
	{
		
		$fields = array(
			'mamon' 			=> $this->input->post('mamon'),
			'tenmon'			=> $this->input->post('tenmon'),
			'sotinchi'			=> $this->input->post('sotinchi'),
			'id_khoa'			=> $this->input->post('makhoa'),
			'edited_by'			=> $this->user->id,
			'status'			=> $this->input->post('status'),
		);
		
		
		$this->db->set($fields);
		
		$this->db->where('id', $id);
		$this->db->update('monhoc');
	}
	
	function delete_monhoc($id)
	{
		$fields = array(
			'status' 			=> 0,
			'deleted' 			=> 1,
		);
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('monhoc');
	}
	

	
	public function change_status_monhoc($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('monhoc',$data);		
	}
	
	
	public function search_data($keyword)
	{
		$this->db->select('monhoc.*, khoa.id as khoaid, khoa.makhoa');
		$this->db->from('monhoc','khoa');
		$this->db->join('khoa', 'khoa.id = monhoc.id_khoa');
		$this->db->where('monhoc.deleted',0);
		$this->db->like('monhoc.tenmon', $keyword);
		$this->db->or_like('monhoc.mamon', $keyword);
		$this->db->order_by('monhoc.id', 'DESC');
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}

	public function count_all_monhoc()
	{
		$this->db->select('id');
		$this->db->from('monhoc');
		return $this->db->count_all_results();
	}
}
// END giaovien_model.php
/* Location: ./application/models/giaovien_model.php */
